using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Input=Microsoft.Xna.Framework.Input; // to provide shorthand to clear up ambiguities

namespace J2i.Net.XnaXboxController
{

    public partial class XnaInputForm : Form
    {
        udpSocket myUdpSocket;
        xmlSettings myXmlSettings;

        gamepad myGamepad1;
        gamepad myGamepad2;
        gamepad myGamepad3;
        gamepad myGamepad4;

        int vibrationCountdown = 0;

        public XnaInputForm()
        {
            InitializeComponent();
            myXmlSettings = new xmlSettings();
            string ip = myXmlSettings.getIp();
            //label1.Text = "Send to IP: " + ip;
            myUdpSocket = new udpSocket(ip, 44000);

            myGamepad1 = new gamepad(this, PlayerIndex.One,   myUdpSocket);
            myGamepad2 = new gamepad(this, PlayerIndex.Two,   myUdpSocket);
            myGamepad3 = new gamepad(this, PlayerIndex.Three, myUdpSocket);
            myGamepad4 = new gamepad(this, PlayerIndex.Four,  myUdpSocket);
        }

        private void StopAllVibration()
        {    
            GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);
            GamePad.SetVibration(PlayerIndex.Two, 0.0f, 0.0f);
            GamePad.SetVibration(PlayerIndex.Three, 0.0f, 0.0f);
            GamePad.SetVibration(PlayerIndex.Four, 0.0f, 0.0f);
        }
        
        private void vibrate1_Click_1(object sender, EventArgs e)
        {
            myGamepad1.setVibration("test");
        }

        private void vibrate2_Click_1(object sender, EventArgs e)
        {
            myGamepad2.setVibration("test");
        }

        private void vibrate3_Click_1(object sender, EventArgs e)
        {
            myGamepad3.setVibration("test");
        }

        private void vibrate4_Click_1(object sender, EventArgs e)
        {
            myGamepad4.setVibration("test");
        }

        private void UpdateControllerState()
        {
            myGamepad1.updateData();
            myGamepad2.updateData();
            myGamepad3.updateData();
            myGamepad4.updateData();

            #region shoot buttons
            this.leftTriggerPosition.Value = myGamepad1.leftTriggerPosition;
            this.rightTriggerPosition.Value = myGamepad1.rightTriggerPosition;

            this.leftTriggerPosition2.Value = myGamepad2.leftTriggerPosition;
            this.rightTriggerPosition2.Value = myGamepad2.rightTriggerPosition;

            this.leftTriggerPosition3.Value = myGamepad3.leftTriggerPosition;
            this.rightTriggerPosition3.Value = myGamepad3.rightTriggerPosition;

            this.leftTriggerPosition4.Value = myGamepad4.leftTriggerPosition;
            this.rightTriggerPosition4.Value = myGamepad4.rightTriggerPosition;
            #endregion

            #region move buttons
            this.buttonUp.Checked = myGamepad1.buttonUpChecked;
            this.buttonDown.Checked = myGamepad1.buttonDownChecked;
            this.buttonLeft.Checked = myGamepad1.buttonLeftChecked;
            this.buttonRight.Checked = myGamepad1.buttonRightChecked;

            this.buttonUp2.Checked = myGamepad2.buttonUpChecked;
            this.buttonDown2.Checked = myGamepad2.buttonDownChecked;
            this.buttonLeft2.Checked = myGamepad2.buttonLeftChecked;
            this.buttonRight2.Checked = myGamepad2.buttonRightChecked;

            this.buttonUp3.Checked = myGamepad3.buttonUpChecked;
            this.buttonDown3.Checked = myGamepad3.buttonDownChecked;
            this.buttonLeft3.Checked = myGamepad3.buttonLeftChecked;
            this.buttonRight3.Checked = myGamepad3.buttonRightChecked;

            this.buttonUp4.Checked = myGamepad4.buttonUpChecked;
            this.buttonDown4.Checked = myGamepad4.buttonDownChecked;
            this.buttonLeft4.Checked = myGamepad4.buttonLeftChecked;
            this.buttonRight4.Checked = myGamepad4.buttonRightChecked;
            #endregion

            #region extra buttons
            this.buttonA.Checked = myGamepad1.buttonAChecked;
            this.buttonB.Checked = myGamepad1.buttonBChecked;
            this.buttonX.Checked = myGamepad1.buttonXChecked;
            this.buttonY.Checked = myGamepad1.buttonYChecked;

            this.buttonA2.Checked = myGamepad2.buttonAChecked;
            this.buttonB2.Checked = myGamepad2.buttonBChecked;
            this.buttonX2.Checked = myGamepad2.buttonXChecked;
            this.buttonY2.Checked = myGamepad2.buttonYChecked;

            this.buttonA3.Checked = myGamepad3.buttonAChecked;
            this.buttonB3.Checked = myGamepad3.buttonBChecked;
            this.buttonX3.Checked = myGamepad3.buttonXChecked;
            this.buttonY3.Checked = myGamepad3.buttonYChecked;

            this.buttonA4.Checked = myGamepad4.buttonAChecked;
            this.buttonB4.Checked = myGamepad4.buttonBChecked;
            this.buttonX4.Checked = myGamepad4.buttonXChecked;
            this.buttonY4.Checked = myGamepad4.buttonYChecked;
            #endregion

        }

        //I'm updating the controller display on a timed interval. 
        private void controllerTimer_Tick(object sender, EventArgs e)
        {
            this.UpdateControllerState();
        }

        private void XnaInputForm_Load(object sender, EventArgs e)
        {
            this.ddlController.SelectedIndex = 0;
            this.controllerTimer.Start();
        }

        private void XnaInputForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.StopAllVibration();
        }

        private void ddlController_SelectedIndexChanged(object sender, EventArgs e)
        {
            int team = 1;
            switch (this.ddlController.SelectedIndex)
            {
                case 0: team = 1; break;
                case 1: team = 2; break;
                default: team = 1; break;
            }
            myGamepad1.setTeam(team);
        }

        private void ddlController3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int team = 1;
            switch (this.ddlController3.SelectedIndex)
            {
                case 0: 
                    team = 1; 
                    break;
                case 1: 
                    team = 2; 
                    break;
                default: 
                    team = 1; 
                    break;
            }
            myGamepad3.setTeam(team);
        }

        private void ddlController4_SelectedIndexChanged(object sender, EventArgs e)
        {
            int team = 1;
            switch (this.ddlController4.SelectedIndex)
            {
                case 0: team = 1; break;
                case 1: team = 2; break;
                default: team = 1; break;
            }
            myGamepad4.setTeam(team);
        }

        private void ddlController2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int team = 1;
            switch (this.ddlController2.SelectedIndex)
            {
                case 0: team = 1; break;
                case 1: team = 2; break;
                default: team = 1; break;
            }
            myGamepad2.setTeam(team);
        }

        private void leftTriggerPosition3_Click(object sender, EventArgs e)
        {

        }

        private void checkBox55_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox77_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox78_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox30_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox62_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox83_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox36_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void buttonY2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox82_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void buttonBack3_CheckedChanged(object sender, EventArgs e)
        {

        }




    }
}